package mysql.ddl.queries;

import mysql.Connection;
import mysql.Schema;
import mysql.commands.SqlCommand;

import java.sql.ResultSet;
import java.sql.SQLException;

import static mysql.ddl.SqlStrategy.LOAD_FILE;

/**
 * Created by nuno-rafael-figueiredo on 01-07-2014.
 */
public class SelectPks extends SqlCommand {
    private final Schema schema;

    public SelectPks(Connection connection, Schema schema) {
        super(connection, LOAD_FILE);
        this.schema = schema;
    }

    @Override
    public void execute() {
        try {
            ResultSet resultSet = connection.executeQuery(getSql());
            while (resultSet.next()) {
                schema
                        .getTable(resultSet.getString(1))
                        .getColumn(resultSet.getString(2))
                        .setPrimary();
            }
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }
}
